package com.itboye.sunsun.person.ui;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.controller.LoginedState;
import com.itboye.sunsun.login.ui.LoginActivity;
import com.itboye.sunsun.login.ui.RegistActivity;
import com.itboye.sunsun.login.ui.TimeCount;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ChangeBindActivity extends BaseActivity implements OnClickListener {

	private Button yzmbutton;
	private Button changebutton;
	private TextView phonetext;
	private EditText yzmedit;
	private EditText phoneedit;
	private EditText passwordedit;
	private RelativeLayout backrela;
	private String mobile;
	private TimeCount time;
	private String phonenumber;
	protected String code;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_changebind);
		setStatusBarColor(R.color.top_blue);
		time = new TimeCount(60000, 1000, yzmbutton);// 构造CountDownTimer对象
		Intent intent = getIntent();
		phonenumber = intent.getStringExtra("phonenumber");

		if (!phonenumber.equals(""))

		{
			try {
				String hintphone = phonenumber.substring(0, 3) + "****"
						+ phonenumber.substring(7, 11);
				phonetext.setText(hintphone);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.yzmbutton:
			mobile = phoneedit.getText().toString();
			if (!mobile.equals("")) {
				getyzm();
				time.start();
			} else {
				Toast.makeText(ChangeBindActivity.this, "请输入手机号", 0).show();
			}
			break;
		case R.id.changebutton:
			changebind();
			break;
		case R.id.backrela:
			this.finish();
			break;
		default:
			break;
		}

	}

	public void changebind() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_User_changePhone")
				.param("password", "" + passwordedit.getText().toString())
				.param("code", "" + yzmedit.getText().toString())
				.param("uid",
						SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
				.param("mobile", phoneedit.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						SPUtils.put(App.ctx, null, SPContants.MOBILE, phoneedit
								.getText().toString());
						Toast.makeText(App.ctx, "更换成功", 0).show();
						Intent intent = new Intent(
								PersonSettingActivity.EXITCHANGE);
						sendBroadcast(intent);
						ChangeBindActivity.this.finish();

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, exception.toString(), 0)
									.show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void getyzm() {
		if (yzmedit.getText().toString() == null) {
			Toast.makeText(this, "请输入验证码", 0).show();
			return;
		}
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_Message_send_sms")
				.param("mobile", phoneedit.getText().toString())
				.param("code_type", "4")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						code = response.toString();
						// yzmedit.setText(response);

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, exception.toString(), 0)
									.show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

}
